package com.ruoyi.project.tool.swagger.UserController;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.annotation.SignatureValidation;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.interceptor.annotation.RepeatSubmit;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SysMobileUser;
import com.ruoyi.project.system.domain.SysMomentComment;
import com.ruoyi.project.system.domain.SysMoments;
import com.ruoyi.project.system.service.ISysMobileUserService;
import com.ruoyi.project.system.service.ISysMomentCommentService;
import com.ruoyi.project.system.service.ISysMomentsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 平台瞬间评论Controller
 *
 * @author jjp
 * @date 2020-04-14
 */
@RestController
@Api(description = "用户精彩瞬间评论")
@RequestMapping("/mobileUser/comment")
public class UserMomentCommentController extends BaseController {
    @Autowired
    private ISysMomentCommentService sysMomentCommentService;

    @Autowired
    private ISysMomentsService sysMomentsService;

    @Autowired
    private ISysMobileUserService sysMobileUserService;

    /**
     * 查询平台瞬间评论列表
     */
    @ApiOperation("查询平台瞬间评论列表")
    @SignatureValidation
    @GetMapping("/list")
    public TableDataInfo list(SysMomentComment sysMomentComment) {
        startPage();
        List<SysMomentComment> list = sysMomentCommentService.selectSysMomentCommentList(sysMomentComment);
        return getDataTable(list);
    }

    /**
     * 新增平台瞬间评论
     */
    @ApiOperation("新增平台瞬间评论")
    @SignatureValidation
    @RepeatSubmit
    @Log(title = "平台瞬间评论", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysMomentComment sysMomentComment) {
        String username = SecurityUtils.getUsername();
        SysMobileUser sysMobileUser = sysMobileUserService.selectSysMobileUserByName(username);
        Long mobileUserId = sysMobileUser.getMobileUserId();
        sysMomentComment.setMobileUserId(mobileUserId);
        sysMomentCommentService.insertSysMomentComment(sysMomentComment);
        Long commentId = sysMomentComment.getCommentId();
        List<String> urls = sysMomentComment.getUrl();
        for (int j = 0; j < urls.size(); j++) {
            SysMoments sysMoments = new SysMoments();
            sysMoments.setCommentId(commentId);
            sysMoments.setUrl(urls.get(j));
            sysMomentsService.insertSysMoments(sysMoments);
        }
        int count = sysMomentCommentService.selectCount();
        return toAjax(count);
    }


    /**
     * 获取平台瞬间评论详细信息
     */
    @ApiOperation("获取平台瞬间评论详细信息")
    @SignatureValidation
    @GetMapping(value = "/{commentId}")
    public AjaxResult getInfo(@PathVariable("commentId") Long commentId) {
        SysMomentComment sysMomentComment = sysMomentCommentService.selectSysMomentCommentById(commentId);
        SysMoments sysMoments = new SysMoments();
        sysMoments.setCommentId(commentId);
        List<SysMoments> sysMoments1 = sysMomentsService.selectSysMomentsList(sysMoments);
        Map<Object, Object> map = new HashMap<>();
        map.put(sysMomentComment, sysMoments1);
        return AjaxResult.success(map);
    }
}
